//
//  AwaitClassCell.swift
//  NewXueBa
//
//  Created by Shine on 2019/6/4.
//  Copyright © 2019 jmxQAQ. All rights reserved.
//

import UIKit

class AwaitClassCell: UITableViewCell {

    @IBOutlet weak var classTitle: UILabel!
    @IBOutlet weak var classHead: UIImageView!
    @IBOutlet weak var classState: BCUILabel!
    @IBOutlet weak var classTime: UILabel!
    @IBOutlet weak var courseware: UILabel!
    @IBOutlet weak var invateCode: UILabel!
    @IBOutlet weak var teachName: UILabel!
    @IBOutlet weak var labelOne: UILabel!
    @IBOutlet weak var labelTwo: UILabel!
    @IBOutlet weak var playState: UIImageView!
    
    var courseType: CourseType = .AwaitClasss

    var awaitModel: Course_list? {
        didSet {
            if let model = awaitModel {
                setData(model: model)
            }
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // 头像圆角
        classHead.layer.cornerRadius = 20
        classHead.layer.masksToBounds = true
    }

    fileprivate func setData(model: Course_list) {
        classTitle.text = model.course_info?.course_name
        teachName.text = model.course_info?.createname
        let time = String.getDateFormatString(timeStamp: model.course_info?.course_time ?? 0, format: .TimeType_Hm)
        classTime.text = time
        let url = URL(string: model.teacher_info?.photo_url ?? "")
        classHead.kf.setImage(with: url, placeholder: UIImage(named: "bg_default_head"))
        // 待上课程 还是课程回看
        guard courseType == .AwaitClasss else {
            classState.isHidden = true
            playState.isHidden = false
            // 课程回看
            labelOne.text = "课程时间"
            labelTwo.text = "结束时间"
            invateCode.text = String.getFormatterDuration((Int(model.course_info?.duration ?? 0)))
            let endDuration = (model.course_info?.course_time ?? 0) + (model.course_info?.duration ?? 0)
            let endTime = String.getDateFormatString(timeStamp:endDuration, format: .TimeType_Hm)
            courseware.text = endTime
            return
        }
        //待上课程
        playState.isHidden = true
        labelOne.text = "邀请码"
        labelTwo.text = "主课件"
        invateCode.text = model.course_info?.invitationCode
        courseware.text = "老师准备的课件.ppt"
        // 判断当前的时间是否在课程时间里
        let nowTime = String.getNowTimeString(format: .TimeType_yMd_Hm)
        if let course_time = model.course_info?.course_time, let duration = model.course_info?.duration {
            let starTime = String.getDateFormatString(timeStamp: course_time, format: .TimeType_yMd_Hm)
            let endTime = String.getDateFormatString(timeStamp: (course_time+duration), format: .TimeType_yMd_Hm)
            if nowTime >= starTime && nowTime <= endTime {
                classState.isHidden = false
                classTime.isHidden = true
            } else {
                classState.isHidden = true
                classTime.isHidden = false
            }
        }
        
    }

}
